﻿@{
    ViewBag.Title = "购买";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@Styles.Render("~/Content/css/business")
<nav class="mod_nav">
    <div class="lay_left">
        <a class="nav_back" onclick="turnBack()"><img src="/images/TurnBackNormal.png"></a>
        <span>购买</span>
    </div>
</nav>
<section class="OrderCon">
    <section id="defaultAddress" class="OrderPartOne" onclick="setDeliveryAddr()">
        <div class="OrderPostion" id="validDelliveryAddr">
            <p><span id="deliveryAddrUserName"></span><span id="deliveryAddruserPhone"></span></p>
            <p id="deliveryAddr"></p>
            <img src="@Url.Content("~/")images/OrderEnter.png">
        </div>
    </section>
    <section>
        <div class="OrderTime">
            <h2>收货时间</h2>
            <section>
                <span id="selectedDeliveryTime">一小时内送达</span>
                <a>预约</a>
            </section>
        </div>
    </section>
    <section class="OrderPartTwo">
        <div class="OrderPay">
            <span>微信支付</span>
            <img id="weixinPay" paytypeid="2" src="@Url.Content("~/")images/OrderPayT.png">
        </div>
        <div class="OrderPay">
            <span>货到付款</span>
            <img id="pickedPay" paytypeid="4" src="@Url.Content("~/")images/OrderPayF.png">
        </div>
    </section>
    <section class="OrderPartThree">
        <div class="OrderDistribution">
            <span>配送费</span>
            <span id="deliver_fee">￥0.00</span>
        </div>
        <div class="ShopCarCon">
            <div class="ShopCarWindow">
                <div class="goodsListDoc">
                    <table id="shoppingGoodsList"></table>
                </div>
            </div>
        </div>
    </section>
</section>
<section class="Shopping">
    <section class="ShopPart4 ShopPart3 OrderShop">
        <div style="overflow:hidden;">
            <div class="ShoppingTxt fll">
                <div class="ShoppingMoney">
                    <span>共</span><a href="" id="ShoppingCarTotalPrice">￥26.00</a>
                </div>
            </div>
            <div class="ShoppingAll fll">
                <a onclick="PayOrder()">
                    <span>立即支付</span>
                    <span id ="ShoppingCarDelivery">差￥26.00免配送费</span>
                </a>
            </div>
        </div>
    </section>
</section>
<section class="OrderGray"></section>
<section class="OrderTimeChoose">
    <div class="OrderTimeBtn">
        <a>取消</a>
        <a>确定</a>
    </div>
    <div class="OrderTimeCon">
        <div class="OrderTimeLine"></div>
        <ul class="OrderTimeFir">
            <li>&nbsp</li>
            <li class="OrderTimeFirFocus">今天</li>
            <li>明天</li>
            <li>后天</li>
        </ul>
        <ul class="OrderTimeSec">
        </ul>
    </div>
</section>

<script>
    window.onload = function () {
        loadDeliveryAddrHistory();
        showShoppingGoodsList();
        showShoppingCarFee();
        OrderMgr.setOrderChangeNotifyCallback(orderChangeNotifyCallback);
    }
    var scrollEndCount = 0;
    $(function () {
        //当前浏览器窗口高度 商品类表之外的高度 45 + 54 + 46 + 108 + 59 + 50 = 362
        var currentWindowHeight = $(window).height() - 363;
        var attrHeight = "height:" + currentWindowHeight + "px";
        //当前容器确定高度
        $(".ShopCarCon").attr("style", attrHeight);
        var shopCarWindow = $(".ShopCarWindow");
        var goodsList = $(".goodsListDoc");
        iscroll(shopCarWindow, goodsList, function () { });
    });

    function iscroll(scrollWindow, scrollDoc, scrollCallBack) {
        $(scrollWindow).scroll(function () {
            var scrollTop = $(this).scrollTop();
            var windowHeight = $(this).height();
            var docHeight = $(scrollDoc).height();
            if (scrollTop >= (docHeight - windowHeight)) {
                scrollEndCount++;
                if (scrollCallBack) {
                    scrollCallBack();
                };
                //console.log("scroll end:" + (docHeight - windowHeight - scrollTop));
            } else {
                //console.log("scrollTop:" + screenTop);
                //console.log("scrolling..." + (docHeight - windowHeight - scrollTop));
            }
        });
    }
    //测试时间转换的
    function test() {
        var str = $("#selectedDeliveryTime").text();
        console.log(str);
        var deliveryTime = setDeliveryTime(str);
        console.log(deliveryTime);
        var times = new Array();
        times = deliveryTime.split('-');
        var startTime = Number(times[0]);
        var endTime = Number(times[1]);
        console.log("start" + startTime + " end:" + endTime);
    }
    function setDate(){
        var currentDate = new Date();
        console.log("当前时间：" + currentDate);
        currentDate.setDate(currentDate.getDate() + 1);
        console.log("天数加1：" + currentDate);
        currentDate.setHours(16);
        console.log("小时16：" + currentDate);
        console.log("year:" + currentDate.getFullYear() + " month:" + currentDate.getMonth() + " day:" + currentDate.getDate() + " hour:" + currentDate.getHours());
        var UTCTimeNum = Number(Date.UTC(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), 16));
        console.log("UTC毫秒数：" + UTCTimeNum);
        var currentDate2=new Date(UTCTimeNum);
        console.log("UTC毫秒:" + UTCTimeNum + " 转正常时间:" + currentDate2.toUTCString());
        console.log("2的时间year:" + currentDate2.getUTCFullYear() + " month:" + currentDate2.getUTCMonth() + " day:" + currentDate2.getUTCDate() + " hour:" + currentDate2.getUTCHours());
    }

    function turnBack() {
        OrderMgr.resetOrderChangeNotifyCallback();
        window.location.href = '@Url.Content("~/")' + 'Home/Index';
    }

    function orderChangeNotifyCallback(orderInfo) {
        OrderMgr.resetOrderChangeNotifyCallback();
        //这里做变动（直接跳转到订单页面）
    }

    function loadDeliveryAddrHistory() {
        var deliveryAddrIndex = DeliveryAddrMgr.getDeliveryAddrIndex();
        var deliveryAddrInfo = DeliveryAddrMgr.getDefDeliveryAddr(deliveryAddrIndex);
        if (null != deliveryAddrInfo) {
            $("#deliveryAddrUserName").show();
            $("#deliveryAddrUserName").text(deliveryAddrInfo.username);
            $("#deliveryAddruserPhone").text(deliveryAddrInfo.userphone);
            $("#deliveryAddr").text(deliveryAddrInfo.deliveryaddr);
        } else {
            $("#validDelliveryAddr").html("<p><span>添加收货地址</span><img src=\"/images/OrderEnter.png\" style=\"top:20px;\"></p>");
        }
    }
    //配送费、总价、邮费
    function showShoppingCarFee() {
        var deliveryFee = TerminalMgr.getDeliveryFee();
        $('#deliver_fee').html("￥" + deliveryFee.toFixed(1));

        var totalPrice = ShoppingMgr.getTotalPrice();
        $('#ShoppingCarTotalPrice').html("￥" + (totalPrice+deliveryFee).toFixed(1));

        var flagFallPrice = TerminalMgr.getFlagFallPrice();
        var deliveryHelper;
        if (totalPrice < flagFallPrice) {
            deliveryHelper = "差￥";
            deliveryHelper += (flagFallPrice - totalPrice).toFixed(1);
            deliveryHelper += "免配送费";
            $('#ShoppingCarDelivery').text(deliveryHelper);
        } else {
            deliveryHelper = "免配送费";
            $('#ShoppingCarDelivery').text(deliveryHelper);
        }

    }

    function setDeliveryAddr() {
        //TODO:
        if(UserInfoMgr.isLogIn()){
            window.location.href = '@Url.Content("~/")' + 'DeliveryAddr/Index?redirectUrl=/Buy/Index';
        }
        else {
            window.location.href = '@Url.Content("~/")' + 'User/Login?url=/Buy/Index';
        }
    }

    function showShoppingGoodsList() {
        var shoppingList = ShoppingMgr.getShoppingList();
        if (null != shoppingList && shoppingList.length > 0) {
            var url = '@Url.Content("~/")' + 'Buy/ShowGoodsInfo';
            NativeAjax(shoppingList, url, function (msg) { loadPartialViewResult(msg, $("#shoppingGoodsList"), 1); });
        }
    }

    function getOrderPayType() {
        var aa = $("#weixinPay")[0].src;
        if (aa.indexOf("OrderPayT.png") > 0) {
            return 2;
        }
        else {
            return 4;
        }
    }

    function validOrderCallback(orderPayInfo) {
        //这里表示的是有效订单，接下来就是要支付
        //微信支付，待确定
        //货到付款支付，调用OrderMgr.codPay
        var orderType = getOrderPayType();
        if (OrderMgr == 4) {
            OrderMgr.codPay(orderPayInfo);
            window.onload.herf = '@Url.Content("~/")' + 'Order/OrderDetail';
        }
        //微信支付
        else {

        }
    }

    function invalidOrderCallback() {
        //这里表示是无效订单，需要刷新商品列表
    }

    function PayOrder() {
        //先判断是否登录，送货地址是否有效，购物车中是否有商品
        //ShoppingMgr.preHandleOrder，记得参数中设置上面的回调接口
        if (UserInfoMgr.isLogIn()) {
            window.location.href = '@Url.Content("~/")' + 'DeliveryAddr/Index?redirectUrl=/Buy/Index';
        }
        else {
            if (DeliveryAddrMgr.getDefDeliveryAddr() != DeliveryAddrMgr.invalidDefIndex) {
                var shoppingList=ShoppingMgr.getShoppingList();
                if (shoppingList != null && shoppingList.length > 0) {
                    var str = $("#selectedDeliveryTime").text();
                    var deliveryTime = setDeliveryTime(str);
                    var times = new Array();
                    times = deliveryTime.split('-');
                    var startTime = Number(times[0]);
                    var endTime = Number(times[1]);
                    // (orderPayType, deliveryTimerStart, deliveryTimerStop, validOrderCallback, invalidOrderCallback) 
                    ShoppingMgr.preHandleOrder(getOrderPayType(), startTime, endTime, validOrderCallback, invalidOrderCallback);
                }
                else {
                    console.log("您未选购任何商品，请选购商品后再下单！");
                }
            }
            else {
                console.log("送货地址无效，请选择收货地址！");
            }
        }
    }
</script>